//定义白名单,不需要登陆即可访问的路径 
const whiteList = [
	'/pages/index/index', 
	'/pages/tabBar/take-pictures/take-pictures',
	'/pages/tabBar/pit-avoidance/pit-avoidance',
	'/pages/tabBar/time-axis/time-axis',
	'/pages/index/post-detail',
	'/pages/guide/guide',
	'/pages/product/product',
	'/pages/user/others/others',
	'/pages/user/store/store',
	'/pages/user/release/release',
	'/pages_sub/login/login',
	'/pages_sub/login/select-sex/select-sex',
	'/pages_sub/login/select-tags/select-tags',
	'/pages/common/protocol/protocol',
	'/pages/common/activity/activity',
	'/pages_sub/login/find-password',
	'/pages_sub/webView/webView'
]

//检查路径是否在白名单内 
function checkWhite(url){
	const path = url.split('?')[0]
	return whiteList.indexOf(path) !== -1 
}

//登陆页路径 
const loginPage = "/pages_sub/login/login"

//要封装的路由方法列表 
const list = ['navigateTo', 'redirectTo', 'reLaunch', 'switchTab']

list.forEach(item => {
	//保存原始方法 
	const originalMethod = uni[item]
 
	//重写方法,添加登陆校验 
	uni[item] = function(options){
  
		//检查登陆状态 
		const token = uni.getStorageSync('token')
 
		if(token){
			//已登陆,直接调用原始方法 
			originalMethod(options)
		}else{
			//未登陆,检查是否在白名单内 
			if(checkWhite(options.url)){
				originalMethod(options)
			}else{
				// 未登录且不在白名单内，记录目标页面并跳转到登录页
				uni.setStorageSync('targetUrl', options.url);
				//不在白名单,重定向到登陆页 
				// uni.reLaunch({
				// 	url: loginPage 
				// })
				uni.navigateTo({
					url: loginPage 
				})
			}
		}
	}
})
